package com.thrc.borrow.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.thrc.borrow.entity.Role;
import com.thrc.borrow.service.RoleService;
import com.thrc.borrow.utils.ResponseUtil;
import com.thrc.borrow.utils.ReturnCode;
import com.thrc.borrow.vo.RoleVo;

@Controller
@RequestMapping(value = "/role")
public class RoleController {

	@Autowired
	private RoleService roleService;
	
	@RequestMapping("/goRoleJsp")
	public String goRoleJsp() {

		return "sys/roleList";
	}
	
	@RequestMapping("/load4add")
	public String load4add() {

		return "sys/roleAdd";
	}

	@RequestMapping("/getRoleById")
	public void selectById(HttpServletResponse response, Role t) {

		ResponseUtil.print(response, roleService.selectById(t));
	}
	
	@RequestMapping("/getMenuArrByRole")
	public void getMenuArrByRole(HttpServletResponse response, Role t) {

		ResponseUtil.print(response, roleService.getMenuArrByRole(t));
	}

	@RequestMapping("/selectAllRole")
	public void selectAll(HttpServletResponse response, Role t) {

		try {
			ResponseUtil.print(response, roleService.selectAll(t));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}

	}

	@RequestMapping("/selectRoleListByPage")
	public void selectByPage(HttpServletResponse response, Role t) {

		JSONObject retJson = new JSONObject();
		try {
			int totleCount = roleService.selectCount(t);
			retJson.put("total", totleCount);
			if (totleCount == 0) {
				retJson.put("data", null);
				ResponseUtil.print(response, retJson);
				return;
			}
			List<RoleVo> roleList = roleService.selectByPage(t);
			retJson.put("data", roleList);
			ResponseUtil.print(response, retJson);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/updateRole")
	public void update(HttpServletResponse response, Role t) {

		try {
			roleService.update(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/deleteRole")
	public void delete(HttpServletResponse response, Role t) {

		try {
			roleService.delete(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}

	@RequestMapping("/insertRole")
	public void insert(HttpServletResponse response, Role t) {

		try {
			roleService.insert(t);
			ResponseUtil.print(response, ReturnCode.SUCCESS_CODE);
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.print(response, ReturnCode.FAIL_CODE, e.getMessage());
		}
	}
}
